package com.fansl.koala.quickdev.module.users.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.fansl.koala.quickdev.module.users.entity.SysLog;
import com.fansl.koala.quickdev.module.users.mapper.SysLogMapper;
import com.fansl.koala.quickdev.module.users.service.ISysLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fansl.koala.quickdev.common.bean.PageBean;
import com.fansl.koala.quickdev.common.constants.CommonConstants;
import com.fansl.koala.quickdev.module.users.vo.SysLogVo;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
 * <p>
 * 日志表 服务实现类
 * </p>
 *
 * @author fansl
 * @since 2020-04-09
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements ISysLogService {
    /**
    * 分页查询数据
    *
    * @param page 分页条件
    * @param sysLog   查询参数
    * @return 分页数据
    */
    @Override
    public PageBean<SysLogVo> listPage(PageBean<SysLogVo> page, SysLog sysLog){
        //分页参数
        Page<SysLog> queryPage = new Page<>();
        queryPage.setCurrent(page.getPageNo());
        queryPage.setSize(page.getPageSize());
        if (StrUtil.isNotBlank(page.getSortOrder()) && StrUtil.isNotBlank(page.getSortField())) {
            OrderItem orderItem = new OrderItem();
            orderItem.setAsc(page.getSortOrder().equals(CommonConstants.PAGE_QUERY_ORDER_ASC));
            orderItem.setColumn(StrUtil.toUnderlineCase(page.getSortField()));
            queryPage.setOrders(Collections.singletonList(orderItem));
        }
        //查询条件
        SysLog queryParam = new SysLog();
        LambdaQueryWrapper<SysLog> queryWrapper = Wrappers.lambdaQuery(queryParam);
        Page<SysLog> result = this.page(queryPage, queryWrapper);
        //结果转vo
        List<SysLogVo> data = sysLog2Vo(result.getRecords());
         //转换结果
         page.setData(data);
         page.setTotalCount(result.getTotal());
         page.setTotalPage(result.getPages());
         return page;
    }

    /**
    * Vo对象转换
    * @param sysLogs 源对象
    * @return 目标对象
    */
    private List<SysLogVo> sysLog2Vo(List<SysLog> sysLogs){
        if(sysLogs.isEmpty()){
            return Collections.emptyList();
        }
       return sysLogs.stream().map(sysLog->{
            SysLogVo target = new SysLogVo();
            BeanUtil.copyProperties(sysLog,target);
            return target;
        }).collect(Collectors.toList());
    }

    /**
    * Vo对象转换
    * @param sysLog 源对象
    * @return 目标对象
    */
    private SysLogVo sysLog2Vo(SysLog sysLog){
        SysLogVo target = new SysLogVo();
        BeanUtil.copyProperties(sysLog,target);
        return target;
    }
}
